package lib.bone.admin.controller.oss;


import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.module.oss.NepuResearchFileServiceImpl;
import lib.bone.access.module.oss.domain.NepuResearchFile;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.nepudata.service.ResearchServiceCtl;
import lib.bone.oss.constant.FileServiceTypeEnum;
import lib.bone.oss.domain.entity.FileEntity;
import lib.bone.oss.domain.vo.Filevo;
import lib.bone.oss.service.impl.FileService;
import lib.bone.system.domain.entity.SysUser;
import lib.bone.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author  kindear
 */
@Slf4j
@RestController
@RequestMapping("nepu")
@Api(tags = "科研文件管理")
public class NepuFileController {
    @Autowired
    FileService fileService;

    @Autowired
    SysUserService userService;

    @Autowired
    ResearchServiceCtl researchServiceCtl;

    @Autowired
    NepuResearchFileServiceImpl nepuResearchFileService;

    @Deprecated
    @ApiOperation(value = "按实体ID查询关联文件路径")
    @PostMapping("/file/findPath")
    public ResponseDTO<List<Filevo>> findPathById(String entityId,String researchType) throws Exception {

        log.info("ID为{}的{}",entityId,researchType);
        List<Filevo> filevos = new ArrayList<>();
        Optional<Object> o = researchServiceCtl.getById(entityId, researchType);
        if (o.isPresent()){
            List<NepuResearchFile> nepuResearchFiles = nepuResearchFileService.getList(researchType,entityId);
            if (nepuResearchFiles.size()!=0){
                for (NepuResearchFile nepuResearchFile : nepuResearchFiles) {
                    Filevo filevo = new Filevo();
                    Optional<FileEntity> fileEntityOp = fileService.getFileEntity(nepuResearchFile.getFileId());
                    if (!fileEntityOp.isPresent()){
                        return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"文件ID不存在！");
                    }
                    FileEntity fileEntity = fileEntityOp.get();
                    filevo.setFilleName(fileEntity.getFileName());
                    filevo.setUrl(fileEntity.getFilePath());
                    filevos.add(filevo);
                }
            }
            return ResponseDTO.succData(filevos);
        }

        return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"实体ID不存在！");

    }



//    @WebLog(module = "bone-system")
    @ApiOperation(value = "文件上传(登录校验)", notes = "文件本地上传")
    @PostMapping("/file/upload")
    public ResponseDTO<FileEntity> localUpload(MultipartFile file, @RequestParam(value = "fileName",required = false) String fileName) throws Exception {
        log.info(fileName);
        Long userId = StpUtil.getLoginIdAsLong();
        Optional<SysUser> userOp = userService.getById(userId);
        // 获取学工号
        if (!userOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"登录信息不匹配");
        }
        SysUser user = userOp.get();
        String userName = user.getUserName();
        return fileService.fileUpload(file, FileServiceTypeEnum.LOCAL,userName,fileName);
    }




    @RequestMapping("file/{fileId}")
    @ApiOperation(value = "文件下载(stream)")
    public ResponseEntity<byte[]> downLoadById(@PathVariable("fileId") String fileId, HttpServletRequest request) {
        return fileService.downLoadById(fileId, request);
    }

    /**
     * 文件预览
     */
    @ApiIgnore
    @RequestMapping("file/viewer/{fileId}")
    @ApiOperation(value = "文件预览(stream)")
    public void viewFile(@PathVariable("fileId") String fileId,HttpServletRequest request){

    }

}
